package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HttpClientUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
    private static final String PROXY_URL = "http://api.shenlongip.com/ip?key=2kzq66co&pattern=json&count=1&need=1000&mr=1&protocol=2&sign=2d2690c0850f9792e1a7e31f773e4698";
    private static final int connectionRequestTimeout = 30000;
    private static final int socketTimeout = 30000;
    private static final int connectTimeout = 30000;
    private static RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .setSocketTimeout(socketTimeout) // 服务端相应超时
            .setConnectTimeout(connectTimeout) // 建立socket链接超时时间
            .build();
    //是否开启代理
    private static boolean isOpenProxy = false;
    @Autowired
    private Environment env;

    @PostConstruct
    public void config() {
        isOpenProxy = Boolean.parseBoolean(env.getProperty("proxy_ip.open"));
    }
    public static String doGet(String url, Map<String, String> param) {
        return doGet(url, param, false);
    }

    public static String doGet(String url, Map<String, String> param, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
                // 执行请求
                log.info("请求地址：{}，请求参数：{}", url, param.toString());
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpGet.setConfig(config);
            }
            response = httpclient.execute(httpGet);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("请求结果resultString：{}", resultString);
            }
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static String doGet(String url, Map<String, String> param, Map<String, String> headers) {
        return doGet(url, param, headers, false);
    }

    public static String doGet(String url, Map<String, String> param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
                // 执行请求
                log.info("请求地址：{}，请求参数：{}", url, param.toString());
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpGet.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = httpclient.execute(httpGet);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("请求结果resultString：{}", resultString);
            } else {
                log.info("请求结果 != 200");
            }
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static Map<String, Object> doGetRetCookie(String url, Map<String, String> param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        // 创建Httpclient对象
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        String resultString = "";
        CloseableHttpResponse response = null;
        Map<String, Object> result = new HashMap<>();
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
                // 执行请求
                log.info("请求地址：{}，请求参数：{}", url, param.toString());
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpGet.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = httpclient.execute(httpGet);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            // 判断返回状态是否为200
            Map<String, String> cookiesMap = new HashMap<>();
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("请求结果resultString：{}", resultString);
                List<Cookie> cookies = cookieStore.getCookies();
                for (int i = 0; i < cookies.size(); i++) {
                    cookiesMap.put(cookies.get(i).getName(), cookies.get(i).getValue());
                }
            } else {
                log.info("请求结果 != 200");
            }
            result.put("result", resultString);
            result.put("cookies", cookiesMap);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return result;
    }

    public static String doDel(String url, Map<String, String> param, Map<String, String> headers) {
        return doDel(url, param, headers, false);
    }

    public static String doDel(String url, Map<String, String> param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpDelete httpDelete = new HttpDelete(uri);
            httpDelete.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpDelete.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpDelete.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 执行请求
            log.info("请求地址：{}，请求参数：{}", url, param.toString());
            response = httpclient.execute(httpDelete);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("请求结果resultString：{}", resultString);
            } else {
                log.info("请求结果 != 200");
            }
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static String doGet(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        return doGet(url, new HashMap<>());
    }

    public static String doPost(String url, Map<String, Object> param) {
        return doPost(url, param, false);
    }

    public static String doPost(String url, Map<String, Object> param, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, (String) param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf8");
                httpPost.setEntity(entity);
                log.info("请求地址：{}，请求参数：{}", url, param.toString());
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static String doPost(String url, Map<String, Object> param, Map<String, String> headers) {
        return doPost(url, param, headers, false);
    }

    public static String doPost(String url, Map<String, Object> param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 创建参数列表
            if (!MapUtils.isEmpty(param)) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, (String) param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            log.info("请求地址：{}，请求参数：{}", url, param.toString());
            response = httpClient.execute(httpPost);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static String doPost(String url, JSONObject param, Map<String, String> headers) {
        return doPost(url, param, headers, false);
    }

    public static String doPost(String url, JSONObject param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(param.toJSONString(), ContentType.APPLICATION_JSON);
                httpPost.setEntity(entity);
                log.info("请求地址：{}，请求参数：{}", url, param.toJSONString());
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            log.debug("请求结果response：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static String doPut(String url, JSONObject param, Map<String, String> headers) {
        return doPut(url, param, headers, false);
    }

    public static String doPut(String url, JSONObject param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPut httpPost = new HttpPut(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(param.toJSONString(), ContentType.APPLICATION_JSON);
                httpPost.setEntity(entity);
            }
            // 执行http请求
            log.info("请求地址：{}，请求参数：{}", url, param.toJSONString());
            response = httpClient.execute(httpPost);
            log.debug("请求结果response：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    public static Map<String, Object> doPostRetCookie(String url, JSONObject param, Map<String, String> headers) {
        return doPostRetCookie(url, param, headers, false);
    }

    public static Map<String, Object> doPostRetCookie(String url, JSONObject param, Map<String, String> headers, boolean useProxy) {
        if (StringUtils.isEmpty(url)) {
            return new HashMap<>();
        }
        // 创建Httpclient对象
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse response = null;
        String resultString = "";
        Map<String, Object> result = new HashMap<>();
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            // 设置头
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(param.toJSONString(), ContentType.APPLICATION_JSON);
                httpPost.setEntity(entity);
                log.info("请求地址：{}，请求参数：{}", url, param.toJSONString());
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            log.debug("请求结果response：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
            List<Cookie> cookies = cookieStore.getCookies();
            Map<String, String> cookiesMap = new HashMap<>();
            for (int i = 0; i < cookies.size(); i++) {
                cookiesMap.put(cookies.get(i).getName(), cookies.get(i).getValue());
            }
            result.put("result", resultString);
            result.put("cookies", cookiesMap);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return result;
    }

    public static String doPost(String url) {
        return doPost(url, null);
    }

    public static String doPostJson(String url, String json, Map<String, String> headers) {
        return doPostJson(url, json, headers, false);
    }

    public static String doPostJson(String url, String json, Map<String, String> headers, boolean useProxy) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (useProxy && isOpenProxy) {
                HttpHost httpHost = getHttpProxy();
                if(httpHost == null){
                    log.error("获取代理IP失败");
                    return null;
                }
                RequestConfig config = RequestConfig.custom()
                        .setConnectionRequestTimeout(connectionRequestTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectTimeout(connectTimeout)
                        .setProxy(httpHost)
                        .build();
                httpPost.setConfig(config);
            }
            if (!MapUtils.isEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            log.info("请求地址：{}，请求参数：{}", url, json);
            response = httpClient.execute(httpPost);
            log.debug("请求结果：{}", JSONObject.toJSONString(response));
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            log.info("请求结果resultString：{}", resultString);
        } catch (Exception e) {
            log.error("接口请求异常", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("关闭异常", e);
            }
        }
        return resultString;
    }

    private static HttpHost getHttpProxy() {
        HttpHost httpHost = null;
        int count = 10;
        while (httpHost == null && count > 0){
            String result = HttpClientUtil.doGet(PROXY_URL, null);
            if(StringUtils.isEmpty(result)){
                count--;
                continue;
            }
            JSONObject ret = JSONObject.parseObject(result);
            int code = ret.getIntValue("code");
            if (code == 200 && ret.getJSONArray("data").size() > 0) {
                httpHost = new HttpHost(((JSONObject) ret.getJSONArray("data").get(0)).getString("ip"), ((JSONObject) ret.getJSONArray("data").get(0)).getIntValue("port"), "http");
            }
            count--;
        }
        return httpHost;
    }

    public static void main(String[] str) {
    }
}
